package org.bdware.irs.backend.controller;


import org.bdware.bdoa.authentication.annotation.AuthRequired;
import org.bdware.bdoa.authentication.utils.JwtContext;
import org.bdware.irs.IrsBootWithTcp;
import org.bdware.irs.backend.dto.Organization;
import org.bdware.irs.backend.dto.ResponseWrapper;
import org.bdware.irs.backend.dto.Stat;
import org.bdware.irs.backend.dto.User;
import org.bdware.irs.backend.servicesImpl.IRHelperServiceImpl;
import com.google.gson.Gson;
import com.nimbusds.jose.JOSEException;
import org.bdware.irp.stateinfo.UserStateInfo;
import org.bdware.irs.backend.servicesImpl.IrsServiceRemoteImpl;
import org.bdware.irs.backend.servicesImpl.IrsServiceStandAloneImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * LRS模块
 */
@RestController
@RequestMapping(value = "/doa/irs")
public class LRSController {

    @Autowired
    IRHelperServiceImpl irHelperService;
    @Autowired
    IrsServiceStandAloneImpl irsService;
//    IrsServiceRemoteImpl irsService;


    //生成后缀，可以自定义后缀生成规则
    @RequestMapping(value = "/generateSuffix", method = RequestMethod.GET)
    public ResponseWrapper generateSuffix(String type) {
        return new ResponseWrapper(irHelperService.suffixGenerator(type));
    }

    //获取IRS的前缀信息
    @RequestMapping(value = "/getPrefix", method = RequestMethod.GET)
    public ResponseWrapper getPrefix() {
        return new ResponseWrapper(IrsBootWithTcp.irsConfig.prefix);
    }

    //解析对应的doi
    @RequestMapping(value = "/resolve", method = RequestMethod.GET)
    public ResponseWrapper resolve(String doi) {
        return new ResponseWrapper(new Gson().toJson(irsService.resolve(doi)));
    }


    @RequestMapping(value = "/currentUser", method = RequestMethod.GET)
    public ResponseWrapper getCurrentUser(String doi) throws JOSEException {
        if (doi == null) {
            doi = (String) JwtContext.getDoi();
        }
        User user = new User(irsService.getCurrentUser(doi));
        return new ResponseWrapper<User>(user);
    }

    @RequestMapping(value = "/registerUser", method = RequestMethod.POST)
    public ResponseWrapper<String> registerUser(@RequestBody User user) {

        UserStateInfo userStateInfo = user.toUserStateInfo();
        boolean save = irsService.registerUser(userStateInfo);
        if (!save)
            return new ResponseWrapper("注册失败");
        return new ResponseWrapper("注册成功");
    }


    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    public ResponseWrapper<String> updateUser(@RequestBody User user) {

        UserStateInfo userStateInfo = user.toUserStateInfo();
        boolean save = irsService.updateUser(userStateInfo);
//        if(save != true)
//            return new ResponseWrapper("更新用户信息失败");
        return new ResponseWrapper("更新用户信息成功");
    }


    @RequestMapping(value = "/getRepoAndRegs", method = RequestMethod.GET)
    public ResponseWrapper getRepoAndRegs() {
        return new ResponseWrapper(irsService.getBindedRepoAndRegs());
    }

    @RequestMapping(value = "/getAllUsers", method = RequestMethod.GET)
    public ResponseWrapper getAllUsers() {
        return new ResponseWrapper(irsService.getAllUsers());
    }


    @RequestMapping(value = "/getAllDos", method = RequestMethod.GET)
    public ResponseWrapper getAllDos() {
        return new ResponseWrapper(irsService.getAllStateInfos());
    }


    @RequestMapping(value = "/getValue", method = RequestMethod.GET)
    public ResponseWrapper getValue(String key) {
        return new ResponseWrapper(irsService.getValue(key));
    }


    //进一步分配下一级前缀相关的接口

    @RequestMapping(value = "/generateKey", method = RequestMethod.GET)
    public ResponseWrapper generateKey(String doi) throws JOSEException {
        return new ResponseWrapper(irHelperService.keyGenerator(doi));
    }

    @RequestMapping(value = "/getInfo", method = RequestMethod.GET)
    public ResponseWrapper<Organization> getInfo(String doi) throws JOSEException {
        return new ResponseWrapper(irsService.getOrganization(doi));
    }

    @RequestMapping(value = "/generatePrefix", method = RequestMethod.GET)
    public ResponseWrapper generatePrefix() {
        return new ResponseWrapper(irHelperService.prefixGeneratorInIRS());
    }

    @RequestMapping(value = "/saveInfo", method = RequestMethod.POST)
    public ResponseWrapper saveInfo(@RequestBody Organization organization) {
        irsService.saveOrganization(organization);
        return new ResponseWrapper("保存成功");
    }

    @RequestMapping(value = "/approve", method = RequestMethod.POST)
    public ResponseWrapper approve(@RequestBody Organization organization) {
        organization.setStatus("审核通过");
        irsService.saveOrganization(organization);
        return new ResponseWrapper("保存成功");
    }


    @RequestMapping(value = "/disapprove", method = RequestMethod.POST)
    public ResponseWrapper disapprove(@RequestBody Organization organization) {
        organization.setStatus("待审核");
        irsService.saveOrganization(organization);
        return new ResponseWrapper("保存成功");
    }

    @RequestMapping(value = "/applyForPrefix", method = RequestMethod.POST)
    public ResponseWrapper applyForPrefix(@RequestBody Organization organization) {
        organization.setStatus("待审核");
        irsService.saveOrganization(organization);
        return new ResponseWrapper("保存成功");
    }

    @AuthRequired(required = true)
    @RequestMapping(value = "/updateOrganization", method = RequestMethod.POST)
    public ResponseWrapper updateOrganization(@RequestBody Organization organization) {
        irsService.updateOrganization(organization);
        return new ResponseWrapper("更新成功");
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public ResponseWrapper<List<Organization>> allOrganizations() {
        return new ResponseWrapper(irsService.getAllOrganization());
    }


    /*
     * GET
     * 返回irs中的统计信息
     * 格式为Json串
     * */
    @RequestMapping(value = "/stat", method = RequestMethod.GET)
    public ResponseWrapper getStat() {
        return new ResponseWrapper<>(irsService.getStat());
    }

}
